package com.gxa.jjty.admin.controller;


import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.EvaluateService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.pojo.EvaluateVO;
import com.gxa.jjty.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.naming.Name;
import java.sql.Timestamp;
import java.util.Map;

/**
 * @ClassName EvaluateController
 * @Author: ws
 * @date 2021/12/8 10:06
 */
@RestController
@RequestMapping("/evaluate")
@Api(tags = "评价后台接口")
@Validated
public class EvaluateController {

    @Autowired
    EvaluateService evaluateService;

    @GetMapping("/getAll")
    @ApiOperation("分页全查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "每页记录数")
    })
    public Map getAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size) throws IllegalAccessException {

        PageInfo<EvaluateVO> pageInfo = evaluateService.selectByPage(page, size);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());

        return jsonResult.getDataMap();
    }

    @GetMapping("/delete")
    @ApiOperation("根据订单id删除评价")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id")
    })
    public Map delete(@RequestParam("orderId") Long id)  {

        evaluateService.deleteByOrderId(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @GetMapping("/getById")
    @ApiOperation("根据订单id查询评价信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id")
    })
    public Map getById(@RequestParam("orderId") Long id){

        Map map = evaluateService.selectByOrderId(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(map);

        return jsonResult.getDataMap();
    }

    @GetMapping("/getAllBy")
    @ApiOperation("分页全/带条件模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页记录数",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "from", value = "起始时间",paramType = "query",dataType = "time"),
            @ApiImplicitParam(name = "to", value = "结束时间",paramType = "query",dataType = "time"),
            @ApiImplicitParam(name = "name", value = "用户姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "phyName", value = "陪护员姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "orderNumber", value = "订单编号",paramType = "query",dataType = "String"),
    })
    public Map getByTimeAnd(@RequestParam("page") Integer page, @RequestParam("size") Integer size,
                            @RequestParam(value = "from",required = false) String from,
                            @RequestParam(value = "to",required = false) String to,
                            @RequestParam(value = "name",required = false) String name,
                            @RequestParam(value = "phyName",required = false) String phyName,
                            @RequestParam(value = "orderNumber",required = false) String orderNumber
                            )  {

        Timestamp newFrom = null;
        Timestamp newTo = null;
        if (!StringUtil.isEmpty(from)) {
            newFrom = Timestamp.valueOf(from);
        }
        if (!StringUtil.isEmpty(to)) {
            newTo = Timestamp.valueOf(to);
        }

        PageInfo<EvaluateVO> pageInfo = evaluateService.selectByTimeAnd(page, size, newFrom, newTo,
                name,phyName,orderNumber);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());

        return jsonResult.getDataMap();
    }

}
